package com.yunsoft.gateway.utils;

import java.lang.annotation.Annotation;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtils implements ApplicationContextAware {
	
	private static ApplicationContext ctx;
	@Override
	public void setApplicationContext(ApplicationContext ctx) throws BeansException {
		SpringUtils.ctx = ctx;
	}

	public static Object getBeanForName(String name) {
		if (ctx == null) {
			return null;
		}
		return ctx.getBean(name);
	}

	public static <T> T getBeanForName(String name, Class<T> requiredType) {
		if (ctx == null) {
			return null;
		}
		return ctx.getBean(name, requiredType);
	}
	
	public static <T> Map<String,T> getBeanOfType(Class<T> requiredType) {
		if (ctx == null) {
			return null;
		}
		return ctx.getBeansOfType(requiredType);
	}

	public static Map<String,Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
		if (ctx == null) {
			return null;
		}
		return ctx.getBeansWithAnnotation(annotationType);
	}
	
	public static <T> T getBean(Class<T> requiredType) {
		if (ctx == null) {
			return null;
		}
		return ctx.getBean(requiredType);
	}

	
}
